package info.magnolia.blossom.sample;

import info.magnolia.cms.core.MgnlNodeType;
import info.magnolia.jcr.util.NodeUtil;
import info.magnolia.jcr.util.PropertyUtil;
import info.magnolia.module.blossom.annotation.Area;
import info.magnolia.module.blossom.annotation.TabFactory;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.dialog.TabBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: peterbalazs
 * Date: 2/18/13
 * Time: 9:58 AM
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Template(title = "CV Template", id = "blossomSample:pages/cvtemplate")
public class CVTemplate {

    @RequestMapping("/cvtemplate")
    public String render(final Node page, final ModelMap model) {
        return "pages/cvtemplate.jsp";
    }

    @TabFactory("Content")
    public void contentTab(final TabBuilder tab) {
        tab.addEdit("title", "Title", "");
        tab.addEdit("caption", "Page caption", "This text will appear as the big caption under the menu");
        tab.addCheckbox("hideInNavigation", "Hide in navigation", "Check this box to hide this page in navigation");
    }

    @Area(value = "menu", title = "Menu")
    @Controller
    public static class Menu {

        @RequestMapping("/cvtemplate/menu")
        public String render(final Node page, final ModelMap model) throws RepositoryException {

            final Map<String, String> navigation = new LinkedHashMap<String, String>();
            // add homepage
            final Node homeNode = page.getSession().getNode("/cvhome");
            navigation.put(homeNode.getPath(), PropertyUtil.getString(homeNode, "title", "home"));
            // add each subpage
            for (Node node : NodeUtil.getNodes(homeNode, MgnlNodeType.NT_PAGE)) {
                // hide page if a property is set
                if (!PropertyUtil.getBoolean(node, "hideInNavigation", false)) {
                    navigation.put(node.getPath(), PropertyUtil.getString(node, "title", ""));
                }
            }
            model.put("navigation", navigation);
            return "areas/cvmenu.jsp";
        }

        @TabFactory("Style")
        public void styleTab(final TabBuilder tab) {
        }
    }

    @Area(value = "footer", title = "Footer")
    @Controller
    public static class Footer {

        @RequestMapping("/cvtemplate/footer")
        public String render() {
            return "areas/cvfooter.jsp";
        }

        @TabFactory("Style")
        public void styleTab(final TabBuilder tab) {
        }
    }

    @Area(value = "main", title = "Main Content")
    @Controller
    public static class Main {

        @RequestMapping("/cvtemplate/main")
        public String render() {
            return "areas/cvmain.jsp";
        }

        @TabFactory("Settings")
        public void settingsTab(final TabBuilder tab) {

        }
    }
}
